package com.taptech.doufu.services.personalcenter;

import android.app.Activity;
import android.content.Context;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpUtil;
import com.douhuayuedu.douhua.wxapi.WXEntryActivity;
import com.taptech.doufu.weex.http.TFHttpResponse;
import com.taptech.doufu.weex.http.TFHttpUtil;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 微信登录
 */
public class WeChatService {

    private static final String DEFAULT_URL = "http://" + Constant.HOST_NAME
            + "/index.php/member/social_bind_register";
    private static final String WXCODELOGIN_URL = "http://" + Constant.HOST_NAME
            + "/member/wxcodelogin";

    private static final String TAG = WeChatService.class.getSimpleName();
    private static final String URLGETACCESSTOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token?";
    private static final String URLGETUSERINFO = "https://api.weixin.qq.com/sns/userinfo?";
    private static final String URLGETREFRUSHTOKEN = "https://api.weixin.qq.com/sns/oauth2/refresh_token?";
    private static final String APPID = "wxa4be7882bc9c2109";
    private static final String SECRET = "8e257e3eca74be55a936c6c233ccde20";
    private static final String GRANT_TYPE = "authorization_code";
    private static final String SP_WEIXIN_REFRUSH_TOKEN = "wx_refrush_token";
    public static final int HANDLE_WECHAT_BIND_REGIST = 1288;



    private volatile static WeChatService singleton;


    private Context mContext;
    private ApiClient apiClient;



    private WeChatService() {
        if (apiClient == null) {
            apiClient = new ApiClient();
            apiClient.init(WeMediaApplication.applicationContext);
        }
    }

    public static WeChatService getInstance(Context context) {
        if (singleton == null) {
            synchronized (WeChatService.class) {
                if (singleton == null) {
                    singleton = new WeChatService();
                }
            }
        }
        if (!(context instanceof WXEntryActivity)) {
            singleton.mContext = context;
        }
        return singleton;
    }

    /**
     * 发送微信登录请求
     */
    public void sendWeChatLogin() {
        IWXAPI api = WXAPIFactory.createWXAPI(WeMediaApplication.applicationContext, APPID, true);
        api.registerApp(APPID);
        if (! api.isWXAppInstalled()) {
            UIUtil.toastMessage(mContext, "请先安装微信");
            return;
        }
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "carjob_wx_login";
        api.sendReq(req);
    }


    /**
     * 微信登录，只需要传入code,剩余的工作服务端完成，
     * 暂时没有处理回调数据，只是同步微信数据，后续优化
     * @param code
     */
    public void wxcodelogin(String code, TFHttpResponse response) {
        Map<String, String> params = new HashMap<>();
        params.put("code", code);
        TFHttpUtil.requestPost(WXCODELOGIN_URL, params, response);
    }
}
